package vip.websky.project.user.server;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.websky.project.common.utils.ObjectConvertUtils;
import vip.websky.project.core.log.KingLog;
import vip.websky.project.module.bo.user.SysRoleBO;
import vip.websky.project.module.entity.user.SysRole;
import vip.websky.project.user.dao.RoleDao;

import java.util.List;

/**
 * 系统角色 服务类
 *
 * @Author YangYong E-mail:yang2048@qq.com
 * @Create 2017/9/24-22:16
 **/
@Slf4j
@Service
public class RoleServer extends ServiceImpl<RoleDao, SysRole> {

    @Autowired
    private RoleDao roleDao;

    /**
     * 1.创建角色
     *
     * @param sysRoleBO 角色信息
     * @return SysRoleBO
     */
    @KingLog
    public SysRoleBO insertRole(SysRoleBO sysRoleBO) {
        SysRole sysRole = ObjectConvertUtils.copyToDest(sysRoleBO, SysRole.class);
        Integer result = roleDao.insert(sysRole);
        return result > 0 ? ObjectConvertUtils.copyToDest(sysRole, SysRoleBO.class) : null;
    }

    /**
     * 2.根据条件查找一个角色
     *
     * @param sysRoleBO 唯有条件
     * @return SysRoleBO
     */
    @KingLog
    public SysRoleBO selectRole(SysRoleBO sysRoleBO) {
        EntityWrapper<SysRoleBO> ew = new EntityWrapper<>();
        ew.where("r.ID = {0}", sysRoleBO.getId())
                .and("r.ROLE_STATUS != -1")
                .groupBy("r.id");
        return roleDao.selectRole(ew);
    }

    /**
     * 3.获取角色列表
     *
     * @param sysRoleBO 角色信息
     * @return Page<SysRoleBO>
     */
    @KingLog
    public Page<SysRoleBO> selectRoleList(SysRoleBO sysRoleBO) {
        Page<SysRoleBO> page = new Page<>(sysRoleBO.getCurPage(), sysRoleBO.getPageSize());
        EntityWrapper<SysRoleBO> ew = new EntityWrapper<>();
        ew.where("r.ROLE_STATUS != {0}", "-1")
                .orderBy("id", true)
                .groupBy("r.id");
        //TODO 查询条件
        //if (StringUtils.isNotEmpty(sysRoleBO.getRoleName())) ew.or("`USERNAME` = {0}", sysRoleBO.getRoleName());
        ew.orderBy("ID", false);
        List<SysRoleBO> sysRoleBOList = roleDao.selectRole(page, ew);
        page.setRecords(sysRoleBOList);
        return page;
    }

    /**
     * 4.修改角色信息
     *
     * @param sysRoleBO 角色
     * @return SysRoleBO
     */
    @KingLog
    public SysRoleBO updateRole(SysRoleBO sysRoleBO) {
        SysRole sysRole = ObjectConvertUtils.copyToDest(sysRoleBO, SysRole.class);
        Integer result = roleDao.updateById(sysRole);
        return result > 0 ? ObjectConvertUtils.copyToDest(sysRole, SysRoleBO.class) : null;
    }

    /**
     * 5.删除角色
     *
     * @param sysRoleBO id
     * @return boolean
     */
    @KingLog
    public boolean delRole(SysRoleBO sysRoleBO) {
        SysRole sysRole = ObjectConvertUtils.copyToDest(sysRoleBO, SysRole.class);
        Integer result = roleDao.deleteById(sysRole.getId());
        return result > 0;
    }
}
